using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Unity.VisualScripting;
using UnityEngine;

public class Main : MonoBehaviour
{
    public GameObject manPrefab;

    public BaseMan myHuman;

    public Dictionary<string, BaseMan> otherHumans = new Dictionary<string, BaseMan>();

    // Start is called before the first frame update
    void Start()
    {
        NetManager.AddListener("Enter", OnEnter);
        NetManager.AddListener("Move", OnMove);
        NetManager.AddListener("Leave", OnLeave);
        NetManager.AddListener("List",OnList);
        NetManager.AddListener("Attack",OnAttack);
        NetManager.AddListener("Die",OnDie);
        NetManager.Connect("127.0.0.1", 8888);
        //添加一个角色
        GameObject obj = (GameObject)Instantiate(manPrefab);
        float x = Random.Range(-5, -5);
        float z = Random.Range(-5, 5);
        obj.transform.position = new Vector3(x, 0, z);
        myHuman = obj.AddComponent<CtrlMan>();
        myHuman.desc = NetManager.GetDesc();
        //请求玩家列表
        NetManager.Send("List|");

        //发送协议
        Vector3 pos = myHuman.transform.position;
        Vector3 eul = myHuman.transform.eulerAngles;
        StringBuilder sb = new StringBuilder();
        sb.Append("Enter|"); // 协议类型（用 | 分隔类型与数据，更清晰）
        sb.Append(NetManager.GetDesc()).Append(",");
        sb.Append(pos.x).Append(",");
        sb.Append(pos.y).Append(",");
        sb.Append(pos.z).Append(",");
        sb.Append(eul.y);

        string sendStr = sb.ToString();
        NetManager.Send(sendStr);

    }

    // Update is called once per frame
    void Update()
    {
        NetManager.Update();
    }

    void OnEnter(string msg)
    {
        Debug.Log("OnEnter" + msg);
        //解析参数
        string[] splits = msg.Split(",");
        string desc = splits[0];
        float x = float.Parse(splits[1]);
        float y = float.Parse(splits[2]);
        float z = float.Parse(splits[3]);
        float eulY = float.Parse(splits[4]);
        //是自己
        if (desc == NetManager.GetDesc())
        {
            return;
        }

        //添加一个角色
        GameObject obj = Instantiate<GameObject>(manPrefab);
        obj.transform.position = new Vector3(x, y, z);
        obj.transform.eulerAngles = new Vector3(0, eulY, 0);
        BaseMan b = obj.AddComponent<SyncMan>();
        b.desc = desc;
        otherHumans.Add(desc, b);
    }

    void OnMove(string msg)
    {
        Debug.Log("OnMove" + msg);
        //解析参数
        string[] splits = msg.Split(",");
        string desc = splits[0];
        float x = float.Parse(splits[1]);
        float y = float.Parse(splits[2]);
        float z = float.Parse(splits[3]);
        //移动
        if (!otherHumans.ContainsKey(desc))
        {
            return;
        }
        BaseMan h = otherHumans[desc];
        Vector3 targetPos = new Vector3(x, y, z);
        h.MoveTo(targetPos);
    }

    void OnLeave(string msg)
    {
        Debug.Log("OnLeave" + msg);
        //解析参数
        string[] split = msg.Split(',');
        string desc = split[0];
        //删除
        if (!otherHumans.ContainsKey(desc))
        {
            return;
        }
        BaseMan h = otherHumans[desc];
        Destroy(h.gameObject);
        otherHumans.Remove(desc);
    }

    void OnList(string msgArgs)
    {
        Debug.Log("OnList"+msgArgs);
        //解析参数
        string[] split = msgArgs.Split(",");
        int count = (split.Length - 1) / 6;
        for (int i = 0; i < count; i++)
        {
            string desc = split[i * 6 + 0];
            float x = float.Parse(split[i * 6 + 1]);
            float y = float.Parse(split[i * 6 + 2]);
            float z = float.Parse(split[i * 6 + 3]);
            float eulY = float.Parse(split[i * 6 + 4]);
            int hp = int.Parse(split[i * 6 + 5]);
            //是自己
            if (desc == NetManager.GetDesc())
            continue;//跳过本次
            GameObject obj = Instantiate<GameObject>(manPrefab);
            obj.transform.position = new Vector3(x, y, z);
            obj.transform.eulerAngles = new Vector3 (0, eulY, 0);
            BaseMan h = obj.AddComponent<SyncMan>();
            h.desc = desc;
            otherHumans.Add(desc,h);
        }
    }

    void OnAttack(string msgArgs)
    {
        Debug.Log("OnAttack"+msgArgs);
        //解析参数
        string[] split = msgArgs.Split(',');
        string desc = split[0];
        float eulY = float.Parse(split[1]);
        //攻击动作
        if (!otherHumans.ContainsKey(desc))
        {
            return;
        }

        SyncMan h = (SyncMan)otherHumans[desc];
        h.SyncAttack(eulY);
    }

    void OnDie(string msgArgs)
    {
        Debug.Log("OnDie"+msgArgs);
        string desc = msgArgs;
        //因为自己并不在otherHumans里，所以这里要对自己单独处理
        if (msgArgs == NetManager.GetDesc())
        {
            FindObjectOfType<CtrlMan>().Die();
            return;
        }

        BaseMan man = otherHumans[desc];
        man.Die();
        Debug.Log("Man died");
        
    }
}
